/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package primitives;

import SharedDrawing.WgContent;
import common.XmlConstants;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

public class P_Rectangle extends Primitive 
{
	private Rectangle2D m_rectangle;

	public void initPrimitive(int x, int y, int width, boolean fill, Color color, Color border)
	{
		try 
		{
			m_type = this.getClass().getSimpleName();
			initPrimitive(width, fill, color, border);
			m_rectangle = new Rectangle2D.Float(x, y, 0, 0);
		} 
		catch (Exception err) 
		{
			System.out.println("Erreur d'instanciation: " + err);
		}
	}	
	
	@Override
	public boolean isOverObject(int x, int y) 
	{
		if (m_rectangle.intersects(x - 3, y - 3, 6, 6)) 
		{
			return true;
		}
		return false;
	}
	
	@Override
	public void drawComponent(Graphics2D g) 
	{
		g.setStroke(new BasicStroke(m_width));
		
		if(m_isFilled)
		{
			g.setColor(m_color);
			g.fill(m_rectangle);
		}
		
		g.setColor(m_borderColor);
		g.draw(m_rectangle);                
	}
	
	@Override
	public void setShape(int x, int y, int w, int h) 
	{
		m_rectangle.setFrame(x, y, w, h);
	}
	
	@Override
	public WgContent toWgContent()
    {
		WgContent wg = new WgContent(m_type);
		
		wg.addContent(XmlConstants.primitiveId, String.valueOf(m_elementId));
		
		wg.addContent(XmlConstants.primitiveX, String.valueOf((int)m_rectangle.getX()));
        wg.addContent(XmlConstants.primitiveY, String.valueOf((int)m_rectangle.getY()));
        wg.addContent(XmlConstants.primitiveSizeX, String.valueOf((int)m_rectangle.getWidth()));
        wg.addContent(XmlConstants.primitiveSizeY, String.valueOf((int)m_rectangle.getHeight()));
        
		wg.addContent(XmlConstants.primitiveWidth, String.valueOf(m_width));
        wg.addContent(XmlConstants.primitiveFilled, new Boolean(m_isFilled).toString());
        
		wg.addContent(XmlConstants.primitiveColor, Integer.toHexString(m_color.getRGB()));
        wg.addContent(XmlConstants.primitiveBorderColor, Integer.toHexString(m_borderColor.getRGB()));
        
        return wg;
    }
	
	@Override
	public void fromWgContent(WgContent wg)
    {
		m_rectangle.setFrame(Integer.parseInt(wg.get(XmlConstants.primitiveX)),
				   Integer.parseInt(wg.get(XmlConstants.primitiveY)),
				   Integer.parseInt(wg.get(XmlConstants.primitiveSizeX)),
				   Integer.parseInt(wg.get(XmlConstants.primitiveSizeY)));
		
		m_width = Integer.parseInt(wg.get(XmlConstants.primitiveWidth));
		m_isFilled = Boolean.parseBoolean(wg.get(XmlConstants.primitiveFilled));
		
		m_color = Color.decode("0x" + wg.get(XmlConstants.primitiveColor).substring(2));
		m_borderColor = Color.decode("0x" + wg.get(XmlConstants.primitiveBorderColor).substring(2));  
    }

	@Override
	public Object getShape() {
		return m_rectangle;
	}
}